1 ======================================================================================
2 Windows APPLICATION: CSRunProcessAsUser Overview
3 ======================================================================================
5 //////////////////////////////////////////////////////////////////////////////////////
8 The code sample demonstrates how to run a process as a different user.
11 //////////////////////////////////////////////////////////////////////////////////////
14 Step1. Build this project in Visual Studio 2010.
16 Step2. Run CSRunProcessAsUser.exe.
18 Step3. Fill out the User Name, Domain (if it is an Active Directory user account), and
19 Password textboxes for the user that you want to run as. Alternatively, you can
20 click the "..." button next to the User Name textbox. It will prompt a standard
21 user credential collection dialog. You can fill out the user name and password
24 Step4. Click the "Command..." button and select the program that you want to run as the
25 specified user in Step 3.
27 Step5. Click the "Run Command" button to run the program as the specified user. When
28 the process is started successfully, you will see a message box saying "the
29 process xxx started". You can verify that the process is run as the specified
30 user in Task Manager. When you exit the new process, you will see another
31 message box saying "the process xxx exited".
34 //////////////////////////////////////////////////////////////////////////////////////
37 1. The sample uses the "Process.Start" function to implement running programs with
42 // Check the parameters.
43 if (!string.IsNullOrEmpty(tbUserName.Text) &&
44 !string.IsNullOrEmpty(tbPassword.Text) &&
45 !string.IsNullOrEmpty(tbCommand.Text))
47 SecureString password = StringToSecureString(tbPassword.Text);
48 Process proc = Process.Start(
54 ProcessStarted(proc.Id);
56 proc.EnableRaisingEvents = true;
57 proc.Exited += new EventHandler(ProcessExited);
61 MessageBox.Show("Please fill in the user name, password and command");
64 catch (Win32Exception w32e)
66 ProcessStartFailed(w32e.Message);
69 2. The sample uses the C++/CLI library "Kerr.Credentials" that wraps the call of the
70 native API CredUIPromptForCredentials to gather the user credential. The
71 Kerr.Credentials library provided by Kenny Kerr is downloaded from this MSDN
72 article: http://www.microsoft.com/indonesia/msdn/credmgmt.aspx.
76 using (Kerr.PromptForCredential dialog = new Kerr.PromptForCredential())
78 dialog.Title = "Please specify the user";
79 dialog.DoNotPersist = true;
80 dialog.ShowSaveCheckBox = false;
81 dialog.TargetName = Environment.MachineName;
82 dialog.ExpectConfirmation = true;
84 if (DialogResult.OK == dialog.ShowDialog(this))
86 tbPassword.Text = SecureStringToString(dialog.Password);
87 string[] strSplit = dialog.UserName.Split('\\');
88 if (strSplit.Length == 2)
90 this.tbUserName.Text = strSplit[1];
91 this.tbDomain.Text = strSplit[0];
95 this.tbUserName.Text = dialog.UserName;
102 MessageBox.Show(ex.ToString(), "Error");
105 3. After the new process is started, we register its Exited event so that we can be
106 notified when the process exits.
108 proc.EnableRaisingEvents = true;
109 proc.Exited += new EventHandler(ProcessExited);
112 /// Triggered when the target process is exited.
114 private void ProcessExited(object sender, EventArgs e)
116 Process proc = sender as Process;
119 MessageBox.Show("Process " + proc.Id.ToString() + " exited");
124 //////////////////////////////////////////////////////////////////////////////////////
128 http://msdn.microsoft.com/en-us/library/system.diagnostics.process.start.aspx
130 Credential Management with the .NET Framework 2.0
131 http://www.microsoft.com/indonesia/msdn/credmgmt.aspx
134 //////////////////////////////////////////////////////////////////////////////////////